package com.zhdl.hardware.motor.huichuan.repository.impl;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.zhdl.hardware.motor.huichuan.repository.HcPortCmdHandleDao;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class HcPortCmdHandle implements HcPortCmdHandleDao {

    @Resource
    public BehaviorPerform behaviorPerform;
    public Map<String, HcCmdHandleDaoImpl> daoMap = new ConcurrentHashMap<>();


    public void addPort(String port) {
        HcCmdHandleDaoImpl hcCmdHandleDao = new HcCmdHandleDaoImpl(port, behaviorPerform);
        daoMap.put(port, hcCmdHandleDao);
    }


    /**
     * 汇川电机-电机停止运动
     *
     * @param port
     * @param slaveStationId 从站ID
     */
    @Override
    public void hcMotorStop(String port, String slaveStationId) {
        daoMap.get(port).hcMotorStop(slaveStationId);
    }

    /**
     * 汇川电机-电机伺服使能
     *
     * @param port
     * @param slaveStationId 从站ID
     */
    @Override
    public void hcMotorReady(String port, String slaveStationId) {
        daoMap.get(port).hcMotorReady(slaveStationId);
    }

    /**
     * 汇川电机-电机运动使能
     * 伺服使能+多端位置运动使能
     *
     * @param port
     * @param slaveStationId 从站ID
     */
    @Override
    public void hcMotorRun(String port, String slaveStationId) {
        daoMap.get(port).hcMotorRun(slaveStationId);
    }

    /**
     * 汇川电机-设置单个参数的值
     * 功能码06，写入单个寄存器
     *
     * @param port
     * @param slaveStationId 从站Id
     * @param address        寄存器地址
     * @param value          写入的值
     */
    @Override
    public void hcSetSingleValue(String port, String slaveStationId, String address, int value) {
        daoMap.get(port).hcSetSingleValue(slaveStationId, address, value);
    }

    /**
     * 汇川电机-读取当前绝对位置
     *
     * @param port
     * @param slaveStationId 从站Id
     * @param address        寄存器地址
     * @param callback       回调函数
     */
    @Override
    public void hcReadLocation(String port, String slaveStationId, String address, CommonCallback<byte[]> callback) {
        daoMap.get(port).hcReadLocation(slaveStationId, address, callback);
    }

    /**
     * 汇川电机-电机找原点
     *
     * @param port
     * @param slaveStationId 从站Id
     */
    @Override
    public void hcMotorBackOrigin(String port, String slaveStationId) {
        daoMap.get(port).hcMotorBackOrigin(slaveStationId);
    }

    /**
     * 除渣电机-设置伺服使能
     *
     * @param port
     * @param slaveStationId
     */
    @Override
    public void motorWriteSlagReady(String port, String slaveStationId) {
        daoMap.get(port).motorWriteSlagReady(slaveStationId);
    }

    /**
     * 除渣电机-设置电机移动 伺服使能+电机开始多端运动
     * 仅供测试
     *
     * @param port
     * @param slaveStationId
     */
    @Override
    public void motorWriteSlagRun(String port, String slaveStationId) {
        daoMap.get(port).motorWriteSlagRun(slaveStationId);
    }

    /**
     * 除渣电机-设置电机停止运动
     *
     * @param port
     * @param slaveStationId
     */
    @Override
    public void motorWriteSlagStop(String port, String slaveStationId) {
        daoMap.get(port).motorWriteSlagStop(slaveStationId);
    }

    /**
     * 除渣电机找原点
     *
     * @param port
     * @param slaveStationId
     */
    @Override
    public void motorWriteSlagZero(String port, String slaveStationId) {
        daoMap.get(port).motorWriteSlagZero(slaveStationId);
    }

    /**
     * 读取除渣电机-当前绝对位置
     *
     * @param port
     * @param slaveStationId
     * @param address
     * @param callback
     */
    @Override
    public void motorReadSlagLocation(String port, String slaveStationId, String address, CommonCallback<byte[]> callback) {
        daoMap.get(port).motorReadSlagLocation(slaveStationId, address, callback);
    }

    /**
     * 除渣电机-电机移动距离
     *
     * @param port
     * @param slaveStationId
     * @param address
     * @param moveDist       移动距离
     */
    @Override
    public void setSlagMoveDist(String port, String slaveStationId, String address, int moveDist) {
        daoMap.get(port).setSlagMoveDist(slaveStationId, address, moveDist);
    }

    /**
     * 功能码06
     *
     * @param port
     * @param slaveStationId
     * @param address
     * @param value
     */
    @Override
    public void writeSlag06(String port, String slaveStationId, String address, int value) {
        daoMap.get(port).writeSlag06(slaveStationId, address, value);
    }

    /**
     * 电机当前绝对位置(H0b.07)均与机械原点偏移量(H05.36)
     *
     * @param port
     * @param slaveStationId
     */
    @Override
    public void readSlagOriginDistance(String port, String slaveStationId) {
        daoMap.get(port).readSlagOriginDistance(slaveStationId);
    }
}
